package com.im.linving.imsdkdal.imsdk.nimImp;

import com.im.linving.apputils.functions.Action1;
import com.im.linving.apputils.functions.Action2;
import com.im.linving.imsdkdal.imsdk.IMUserAction;
import com.netease.nimlib.sdk.AbortableFuture;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.netease.nimlib.sdk.friend.FriendService;

import java.util.List;

/**
 * @Author linyong
 * @Date 2016/11/1
 * @Time 10:38
 */
public class Nim_IMUserAction implements IMUserAction {

    @Override
    public void login(String name, String pwd, final Action1<Object> success, final Action2<Object,Object> fail) {
        AbortableFuture<LoginInfo> loginRequest = NIMClient.getService(AuthService.class).login(new LoginInfo(name, pwd));
        loginRequest.setCallback(new RequestCallback() {
            @Override
            public void onSuccess(Object o) {
                success.call(o);
            }

            @Override
            public void onFailed(int i) {
                fail.call(i,"onFailed");
            }

            @Override
            public void onException(Throwable throwable) {
                fail.call(-1,"onException");
            }
        });
    }

    @Override
    public void getFriends(Action1<List<String>> success, Action2<Object, Object> fail) {
        List<String> accounts = NIMClient.getService(FriendService.class).getFriendAccounts(); // 获取所有好友帐号
        if (accounts == null || accounts.isEmpty()) {
            return;
        }
        success.call(accounts);
    }


}
